﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Windows;


//

//using ;

namespace mylivemesh_uploader_v1 
{
    class MyViewModel : INotifyPropertyChanged
    {
        #region EVENTS
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region ATTRIBUTES
        private List<string> sdir_;
        private List<string> files_;
        private FileSystemWatcher fswatcher_;
        private string text;
        private string[] arrFolder;
        #endregion

        #region PROPERTIES
        public string[] ArrFolder
        {
            get { return arrFolder; }
            set
            {
                if (arrFolder != value)
                {
                    arrFolder = value;
                    OnPropertyChanged("ArrFolder");
                }
            }
        }
        
        public string Text
        {
            get { return text; }
            set
            {
                if (text != value)
                {
                    text = value;
                    OnPropertyChanged("Text");
                }
            }
        }
        #endregion

        #region COMMANDS_RELAYERS
        public RelayCommand UpOnClickCmd { get; set; }
        public RelayCommand SelectOnClickCmd { get; set; }
        #endregion

        #region COMMANDS
        private void InitCommands()
        {
            this.UpOnClickCmd = new RelayCommand(e =>
            {
                string nbFiles = this.files_.Count().ToString();
                string messageBoxText = "Do you want to upload " + nbFiles + " files ?";
                string caption = "Question";
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                MessageBoxResult result = System.Windows.MessageBox.Show(messageBoxText, caption, button, icon);
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        /*upload files !*/

                        break;
                    case MessageBoxResult.No:
                        break;
                }

            });

            this.SelectOnClickCmd = new RelayCommand(e =>
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                DialogResult result = fbd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.sdir_ = new List<string>();
                    this.files_ = new List<string>();
                    Text = fbd.SelectedPath;
                    this.getFilesInFolder(Text);
                    this.DirSearch(Text);
                }
            });
        }
        #endregion

        #region METHODS
        private void getFilesInFolder(string fo)
        {
            foreach (string f in Directory.GetFiles(fo))
            {
                this.files_.Add(f);
            }
        }

        private void DirSearch(string sDir)
        {
            try
            {
                ArrFolder = Directory.GetDirectories(sDir);
            }
            catch { }
  
            foreach (string fo in arrFolder)
            {
                try
                {
                    this.sdir_.Add(fo);
                    this.getFilesInFolder(fo);
                    this.DirSearch(fo);
                }
                catch { }
            }
        }

        private bool IsADirectory(string path)
        {
            bool bIsDirectory = true;

            try
            { string[] subfolders = Directory.GetDirectories(path); }
            catch
            { bIsDirectory = false; }
            return bIsDirectory;
        }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(p));
        }
        #endregion

        #region CTOR
        public MyViewModel()
        {
            ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
            bool res = sc.connect("e", "e");
            this.text = "Selected folder Path..";
            this.arrFolder = null;
            this.InitCommands();
        }
        #endregion
    }
}
